package com.zjp.annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: zjp
 * @date: 2023-12-02
 * @description: 测试 @MyTest 注解，让包含了 @MyTest 注解的方法执行
 */
public class TestMyTestAnnotation {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        TestMyTestAnnotation a = new TestMyTestAnnotation();

        // 拿到类的信息
        Class<TestMyTestAnnotation> clazz = TestMyTestAnnotation.class;
        // 拿到所有方法
        Method[] methods = clazz.getDeclaredMethods();
        for (Method method : methods) {
            // 判断 MyTest 注解是否在当前方法上
            if (method.isAnnotationPresent(MyTest.class)){ // 让包含了 @MyTest 注解的方法执行
                method.setAccessible(true);
                method.invoke(a); // 调用方法
            }
        }
    }
    @MyTest
    public void test1(){
        System.out.println("test1...");
    }
    public void test2(){
        System.out.println("test2...");
    }
    public void test3(){
        System.out.println("test3...");
    }
    @MyTest
    public void test4(){
        System.out.println("test4...");
    }
}
